package com.rongshu.view.discovery.presenter;

import android.content.Context;

import com.rongshu.R;
import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.BaseActivity;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.discovery.bean.IsAtUserBlackResponse;
import com.rongshu.view.discovery.bean.UserInfoResponse;
import com.rongshu.view.discovery.contract.UserHomePageView;
import com.rongshu.widget.BanyanDialog;
import com.widget.toast.ToastUtils;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;
import rx.functions.Action0;

/**
 * created by zhengweis on 2018/11/16.
 * description：
 */
public class UserHomePagePresenter extends BasePresenter<UserHomePageView> {
    private UserInfoResponse response;
    private BanyanDialog banyanDialog;
    private BaseActivity activity;

    public UserHomePagePresenter(Context context) {
        super(context);
        activity = (BaseActivity) mContext;
    }

    /**
     * 判断用户是否把我拉黑
     */
    public void isAtUserBlack(final int userId) {
        activity.showLoading(true);
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("user_id_to", userId);
        RetorfitService.isAtUserBlack(UserInfoUtils.getUserId(), userId, Common.encryptionSign(map))
                .compose(getView().<IsAtUserBlackResponse>bindToLife())
                .doOnSubscribe(new Action0() {
                    @Override
                    public void call() {

                    }
                })
                .subscribe(new Subscriber<IsAtUserBlackResponse>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        activity.showLoading(false);
                        ToastUtils.show("请求异常，请重试");
                        getView().noPersimmion();
                    }

                    @Override
                    public void onNext(IsAtUserBlackResponse object) {
                        if (object.getErrCode() == 0){
                            getUserMsg(userId);
                        } else {
                            activity.showLoading(false);
                            ToastUtils.show(object.getErrMsg());
                            getView().noPersimmion();
                        }
                    }
                });
    }

    public void getUserMsg(int othreUserId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("user_id_to", othreUserId);
        RetorfitService.getOtherUserInfo(UserInfoUtils.getUserId(), othreUserId,
                Common.encryptionSign(map))
                .compose(getView().<UserInfoResponse>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<UserInfoResponse>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {
                activity.showLoading(false);

            }

            @Override
            public void onNext(UserInfoResponse object) {
                activity.showLoading(false);
                response = object;
                getView().callBackUserMsg(object);
            }
        });
    }

    public void addUserToBlackList(String option) {
        BanyanDialog.Builder builder = new BanyanDialog.Builder(mContext);
        builder.setTitle(option + "黑名单");
        builder.setContent("是否将该用户" + option + "黑名单");
        builder.setLeft("取消");
        builder.setRight("确定");
        builder.setDialogOnClick(new BanyanDialog.Builder.onClickListener() {
            @Override
            public void leftClickListener() {
                banyanDialog.dismiss();
            }

            @Override
            public void rightClickListener() {
                blackListOption();
                banyanDialog.dismiss();
            }
        });
        banyanDialog = builder.create();
        banyanDialog.show();
    }

    public void blackListOption() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("user_id_to", response.getResponse().getUser_id_to());
        RetorfitService.blackListOption(UserInfoUtils.getUserId(), response.getResponse().getUser_id_to(),
                Common.encryptionSign(map))
                .compose(getView().bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<Object>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(Object object) {
                getView().blackListOption();
            }
        });
    }


    //关注
    public void ConcernWith() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("contribute_id", 0);
        map.put("user_id_to", response.getResponse().getUser_id_to());
        RetorfitService.ConcernWith(UserInfoUtils.getUserId(), 0, response.getResponse().getUser_id_to(),
                Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                if (object.getErrCode() == 0) {
                    ToastUtils.show(mContext.getString(R.string.add_attention_success));
                    getView().concernSuccess();
                } else {
                    ToastUtils.show(R.string.attention_no_permission);
                }
            }
        });
    }

    //取消关注
    public void cancelConcernWith() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("contribute_id", 0);
        map.put("user_id_to", response.getResponse().getUser_id_to());
        RetorfitService.cancelConcernWith(UserInfoUtils.getUserId(), 0, response.getResponse().getUser_id_to(),
                Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife()).doOnSubscribe(new Action0() {
            @Override
            public void call() {

            }
        }).subscribe(new Subscriber<INetBean>() {
            @Override
            public void onCompleted() {

            }

            @Override
            public void onError(Throwable e) {

            }

            @Override
            public void onNext(INetBean object) {
                if (object.getErrCode() == 0) {
                    ToastUtils.show(mContext.getString(R.string.cancel_attention_success));
                    getView().concernSuccess();
                } else {
                    ToastUtils.show(R.string.attention_no_permission);
                }
            }
        });
    }
}
